package com.zhongying.jf.serviceImpl;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;
import com.zhongying.jf.mapper.CallListMapper;
import com.zhongying.jf.mapper.CallListRemarksMapper;
import com.zhongying.jf.model.CallList;
import com.zhongying.jf.model.CallListRemarks;
import com.zhongying.jf.service.BankInfoService;
import com.zhongying.jf.service.CustomerService;
import com.zhongying.jf.exception.ZhongyingiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
    public static final Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Autowired
    private CallListMapper callListMapper;
    @Autowired
    private CallListRemarksMapper callListRemarksMapper;
    @Autowired
    private BankInfoService bankInfoService;


    @Override
    public List<Map<String, Object>> queryMyList(Map<String, Object> params) throws ZhongyingiException {
        logger.info("首頁列表查詢參數:"+params.toString());
        List<Map<String, Object>> callList = callListMapper.queryMyList(params);
        callList.stream().forEach(item->{
            String id = item.get("id").toString();
            CallListRemarks callListRemarks = new CallListRemarks();
            callListRemarks.setCallListId(Long.valueOf(id));
            int bid = callListRemarksMapper.selectCount(callListRemarks);
            if(bid==0){
                item.put("bid","无");
            }
            String cusId = item.get("cusId").toString();
            Map<String,Object> nameMobail = callListMapper.getNameMobail(cusId);
            if(nameMobail!=null && !nameMobail.isEmpty()  ){
                if(nameMobail.get("realname")!=null && !nameMobail.get("realname").equals("")){
                    String realname = nameMobail.get("realname").toString();
                    realname =  realname.substring(0,1);
                    nameMobail.put("realname",realname+"XX");
                }
                if(nameMobail.get("mobile")!=null && !nameMobail.get("mobile").equals("")){
                    String mobile = nameMobail.get("mobile").toString().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                    nameMobail.put("mobile",mobile);
                }
                item.put("name",nameMobail.get("realname"));
                item.put("mobile",nameMobail.get("mobile"));
            }
        });
        return callList;
    }

    @Override
    public int queryMyListCount(Map inMap) {
        return callListMapper.queryMyListCount(inMap);
    }

    @Override
    public List<Map<String, Object>> getBzById(Map inMap) {
        List<Map<String,Object>> list = callListRemarksMapper.getBzById(inMap);
        return list;
    }

    @Override
    public int getBzByIdCount(Map inMap) {
        return callListRemarksMapper.getBzByIdCount(inMap);
    }

    @Override
    public List<Map<String, Object>> queryCusList(Map inMap) {
        List<Map<String,Object>> list = callListMapper.queryCusList(inMap);
        return list;
    }

    @Override
    public int queryCusListCount(Map inMap) {
        return callListMapper.queryCusListCount(inMap);
    }

    @Override
    public int addReForCall(Map inMap) {
        Long id = Long.valueOf(inMap.get("id").toString());
        String remark = inMap.get("remark").toString();
        String userId = inMap.get("userId").toString();
        CallList callList01 = new CallList();
        callList01.setId(id);
        CallList callList = callListMapper.selectOne(callList01);
        CallListRemarks callListRemarks = new CallListRemarks();
        //客户id
        callListRemarks.setCustomerId(callList.getCustomerId());
        callListRemarks.setCallListId(id);
        callListRemarks.setCallRemarks(remark);
        callListRemarks.setCallRemarksEdittime(new Date());
        callListRemarks.setCallEditEmpId(Integer.valueOf(userId));
        int insert = callListRemarksMapper.insert(callListRemarks);
        return insert;
    }

    @Override
    public Map<String, Object> getCusInfo(String cusId) {
        Map<String, Object> cusInfo = callListMapper.getCusInfo(cusId);
        Map<String, String> bankMap = bankInfoService.getBankMap();
        String bankNo = cusInfo.get("bankNo").toString();
        String bankName = bankMap.get(bankNo);
        cusInfo.put("bankName",bankName);
        return cusInfo;
    }

    @Override
    public List<Map<String, Object>> queryRechargeListSS(Map inMap) {
        List<Map<String, Object>> list = callListMapper.queryRechargeListSS(inMap);
        Map<String, String> bankMap = bankInfoService.getBankMap();
        list.forEach(item->{
            String bankNo = item.get("bankNo").toString();
            String bankName = bankMap.get(bankNo);
            item.put("bankName",bankName);
        });
        return list;
    }

    @Override
    public int queryRechargeListSSOne(Map inMap) {
        return callListMapper.queryRechargeListSSOne(inMap);
    }

    @Override
    public List<Map<String, Object>> queryWithDraws(Map inMap) {
        List<Map<String, Object>> list = callListMapper.queryWithDraws(inMap);
        Map<String, String> bankMap = bankInfoService.getBankMap();
        list.forEach(item->{
            String bankNo = item.get("bankNo").toString();
            String bankName = bankMap.get(bankNo);
            item.put("bankName",bankName);
        });
        return list;
    }

    @Override
    public int queryWithDrawsCount(Map inMap) {
        return callListMapper.queryWithDrawsCount(inMap);
    }
}
